/*************************************
Copyright 2011 Chinua Iloabachie, Pradnya Khutafale, Siyi Liu, Junichi Suzuki, and The University of Massachusetts, Boston (http://code.google.com/p/tabletuml/)


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 
*************************************/




package touchMe.baseClass;

 

import android.graphics.PointF;

public class RelElement extends Element {

	private Element from;
	private Element to;
	private int x1,y1;
	private int x2,y2;
	
	public void getAdditonal_Attributes()
	{
		String strAttributes = getAdditionalAttributes().toString().trim();
		String attr[]=strAttributes.split(";");
		x1=Integer.parseInt(attr[0]);
		y1=Integer.parseInt(attr[1]);
		x2=Integer.parseInt(attr[2]);
		y2=Integer.parseInt(attr[3]);
		
	}
	
	public Element getFrom() {
		return from;
	}


	public void setFrom(Element from) {
		this.from = from;
	}


	public Element getTo() {
		return to;
	}


	public void setTo(Element to) {
		this.to = to;
	}

	public RelElement(Element from, Element to) {
		super();
		this.from = from;
		this.to = to;
	}
	
	public RelElement(float x,float y,float w,float h, ElementType type)
	{
	 super(x,y,w,h,type);	
	  
	}
	
	public static PointF[] calculateArrowCoOrdinates(float fromX,float fromY,float toX,float toY)
	{
		PointF a1,a2;
		PointF diffA = new PointF(-fromX
				+ toX, -fromY
				+ toY);
		PointF diffB1 = new PointF(diffA.y, -diffA.x);
		PointF diffB2 = new PointF(-diffB1.x, -diffB1.y);
		  a1 = new PointF(2 * diffA.x + diffB1.x, 2 * diffA.y
				+ diffB1.y);
	  a2 = new PointF(2 * diffA.x + diffB2.x, 2 * diffA.y
				+ diffB2.y);
		a1 = Constants.normalize(a1, 11);
		a2 = Constants.normalize(a2, 11);
		
		PointF arrowCo[]= new PointF[2] ;
		arrowCo[0]=a1;
		arrowCo[1]=a2;
		
		return arrowCo;
	}
	/* This code is copied from UmLet Relation class (paintEnity method)
	 * 
	 */
	@Override
	public  void reCalculateCoOrdinates()
	{
		/**
		 * Change Size of relation to fit the relations size
		 * Must be made to resize the relation automatically during draging an endpoint
		 */

		// (minx,miny) is the upper left end and (maxx,maxy) is the lower right end of the relation
		int minx = Integer.MAX_VALUE;
		int miny = Integer.MAX_VALUE;
		int maxx = Integer.MIN_VALUE;
		int maxy = Integer.MIN_VALUE;
		 
			
			minx = Math.min(minx, x1);
			miny = Math.min(miny, y1);
			maxx = Math.max(maxx, x1);
			maxy = Math.max(maxy, y1);
			
			// Subtract or add the SELECT RECTANGEL SIZE to avoid cutting the circles at the end of the relation
			minx = (int) Math.min(minx, x1 - Constants.delta*2 * 1);
			miny = (int) Math.min(miny, y1 - Constants.delta*2 * 1);
			maxx = (int) Math.max(maxx, x1 + Constants.delta*2 * 1);
			maxy = (int) Math.max(maxy, y1 + Constants.delta*2 * 1);
			
			minx = Math.min(minx, x2);
			miny = Math.min(miny, y2);
			maxx = Math.max(maxx, x2);
			maxy = Math.max(maxy, y2);
			
			// Subtract or add the SELECT RECTANGEL SIZE to avoid cutting the circles at the end of the relation
			minx = (int) Math.min(minx, x2 - Constants.delta*2 * 1);
			miny = (int) Math.min(miny, y2 - Constants.delta*2 * 1);
			maxx = (int) Math.max(maxx, x2 + Constants.delta*2 * 1);
			maxy = (int) Math.max(maxy, y2 + Constants.delta*2 * 1);

			 // all this logic from Umlet
		
		 
			minx = Math.min(minx, x1);
			miny = Math.min(miny, y1);
			maxx = Math.max(maxx, x1);
			maxy = Math.max(maxy, y1);
			
			minx = Math.min(minx, x2);
			miny = Math.min(miny, y2);
			maxx = Math.max(maxx, x2);
			maxy = Math.max(maxy, y2);
		 

		// BUGFIX ZOOM: We must consider the gridsize for the min and max value to avoid rounding errors
		// Therefore we subtract or add the difference to the next possible value
		int gridSize = 10;
		minx -= (minx % gridSize);
		miny -= (miny % gridSize);

		// Subtract gridSize another time to avoid a too small selection area for the relation-selection circle
		minx -= gridSize;
		miny -= gridSize;

		maxx += (maxx % gridSize);
		maxy += (maxy % gridSize);

		if ((maxx != 0) || (maxy != 0)) {
			//looks stupid, but do as done in Umlet
			this.setW(this.getW()+maxx - getW());
			this.setY(this.getH()+maxy - getH());
			//this.changeSize(maxx - getWidth(), maxy - getHeight());
		}
		if ((minx != 0) | (miny != 0)) {
			this.setX(getX()+minx);
			this.setY(getY()+miny);
			//this.changeLocation(minx, miny);
			//this.changeSize(-minx, -miny);
			this.setW(this.getW()-minx);
			this.setY(this.getH()-miny);
			//for (int i = 0; i < getLinePoints().size(); i++) {
			//	Point p = getLinePoints().elementAt(i);
			//	p.x += -minx;
			//	p.y += -miny;
			
			this.x1=x1-minx;
			this.y1=y1-miny;
			this.x2=x2-minx;
			this.y2=y2-miny;
			
			StringBuffer attr= new StringBuffer(x1+";"+y1+";"+
					                            x2+";"+y2);
			this.setAdditionalAttributes(attr);
			}
		}
	}
	

